package com.micro.train.controller;

import com.micro.train.entity.TrainMeal;
import com.micro.train.service.TrainMealService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 火车餐控制器
 */
@Tag(name = "火车餐管理", description = "火车餐相关接口")
@RestController
@RequestMapping("/api/meals")
public class TrainMealController {
    
    @Autowired
    private TrainMealService mealService;
    
    @Operation(summary = "添加餐品信息")
    @PostMapping
    public ResponseEntity<TrainMeal> addMeal(@RequestBody TrainMeal meal) {
        TrainMeal savedMeal = mealService.addMeal(meal);
        return ResponseEntity.ok(savedMeal);
    }
    
    @Operation(summary = "更新餐品信息")
    @PutMapping("/{mealId}")
    public ResponseEntity<TrainMeal> updateMeal(@PathVariable Long mealId, @RequestBody TrainMeal meal) {
        TrainMeal updatedMeal = mealService.updateMeal(mealId, meal);
        return ResponseEntity.ok(updatedMeal);
    }
    
    @Operation(summary = "删除餐品信息")
    @DeleteMapping("/{mealId}")
    public ResponseEntity<Void> deleteMeal(@PathVariable Long mealId) {
        mealService.deleteMeal(mealId);
        return ResponseEntity.noContent().build();
    }
    
    @Operation(summary = "获取所有餐品信息")
    @GetMapping
    public ResponseEntity<List<TrainMeal>> getAllMeals() {
        List<TrainMeal> meals = mealService.getAllMeals();
        return ResponseEntity.ok(meals);
    }
    
    @Operation(summary = "根据ID获取餐品信息")
    @GetMapping("/{mealId}")
    public ResponseEntity<TrainMeal> getMealById(@PathVariable Long mealId) {
        TrainMeal meal = mealService.getMealById(mealId);
        return ResponseEntity.ok(meal);
    }
    
    @Operation(summary = "根据列车ID获取餐品列表")
    @GetMapping("/train/{trainId}")
    public ResponseEntity<List<TrainMeal>> getMealsByTrainId(@PathVariable Long trainId) {
        List<TrainMeal> meals = mealService.getMealsByTrainId(trainId);
        return ResponseEntity.ok(meals);
    }
    
    @Operation(summary = "根据餐品种类查询")
    @GetMapping("/type/{mealType}")
    public ResponseEntity<List<TrainMeal>> getMealsByType(@PathVariable String mealType) {
        List<TrainMeal> meals = mealService.getMealsByType(mealType);
        return ResponseEntity.ok(meals);
    }
    
    @Operation(summary = "根据列车ID和餐品种类查询")
    @GetMapping("/train/{trainId}/type/{mealType}")
    public ResponseEntity<List<TrainMeal>> getMealsByTrainIdAndType(
            @PathVariable Long trainId,
            @PathVariable String mealType) {
        List<TrainMeal> meals = mealService.getMealsByTrainIdAndType(trainId, mealType);
        return ResponseEntity.ok(meals);
    }
    
    @Operation(summary = "查询可用的餐品")
    @GetMapping("/available/train/{trainId}")
    public ResponseEntity<List<TrainMeal>> getAvailableMeals(@PathVariable Long trainId) {
        List<TrainMeal> meals = mealService.getAvailableMeals(trainId);
        return ResponseEntity.ok(meals);
    }
    
    @Operation(summary = "更新餐品库存")
    @PutMapping("/{mealId}/stock")
    public ResponseEntity<Boolean> updateStock(
            @PathVariable Long mealId,
            @RequestParam Integer quantity) {
        boolean success = mealService.updateStock(mealId, quantity);
        return ResponseEntity.ok(success);
    }
    
    @Operation(summary = "批量处理餐品库存")
    @PostMapping("/inventory/process")
    public ResponseEntity<Boolean> processMealInventory(@RequestBody List<Map<String, Object>> items) {
        try {
            boolean allSuccess = true;
            for (Map<String, Object> item : items) {
                Long mealId = Long.valueOf(item.get("mealId").toString());
                Integer quantity = Integer.valueOf(item.get("quantity").toString());
                boolean success = mealService.updateStock(mealId, quantity);
                if (!success) {
                    allSuccess = false;
                }
            }
            return ResponseEntity.ok(allSuccess);
        } catch (Exception e) {
            return ResponseEntity.ok(false);
        }
    }
}
